<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Test CHTML</title>
 </head>
 <body>
<?php
/*'****************************************************************************\
    CHTML 0.2.1
 	Copyright (C) 2007, 2008 Alexey Ilyin

 	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published
	by the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.

 	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

 	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

\******************************************************************************/
require 'cform.inc.php';
assert_options (ASSERT_WARNING,  1);
assert_options (ASSERT_CALLBACK, NULL); //DEBUG//

define('DEBUG',     1);
define('FORM_NEXT', 'next');
define('FORM_REF',  'ref');

/**
 *  @test   This is a use case of CFormProcessor.
 */ 
class CTestForm extends CFormProcessor {

    function test ($form) {
        $form->element("#HEADER", 'set', 'nodeValue', 'Test');
        $form->element('opt', 'addOptions', array('opt1', 'opt2'));
        $form->element('#test', 'cloneElements', array($this, 'cloneTest'),
            array('opt3', 'opt4'));

        self::print_($form);
    }

    function show ($form) {
        $form->element('#HEADER', 'set', 'nodeValue', 'Show');

        self::print_('<pre>'.print_r($_REQUEST, 1).'</pre>');
        self::print_($form);
    }

    function ok ($form) {
        $this->close();
    }

    /**
     *  @test   This is a use case of CHTMLBase::cloneElements.
     *  @param  $base   The base element invoked cloneElements.
     *  @param  $elem   => NULL asks to find initial element to clone.
     *  @param  $elem   => !NULL is just for setup.
     *  @param  $arg    The parameter passed by call.
     */
    static function cloneTest ($base, $elem, &$arg) {
        //  find first element
        $elem or $elem = $base->element('~p', 0);

        //  setup elements
        list($key, $value) = each($arg);
        //$elem->element('~input', 'offsetSet', 'name', "test[k$key]");
        $elem->element('~input', 'offsetSet', 'id', "test$key");
        $elem->element('~label', 'offsetSet', 'for', "test$key");
        $elem->element('~input', 'offsetSet', 'value', $key);
        $elem->element('~label', 'set', 'nodeValue', $value);
        
        return current($arg) ? $elem : NULL;
    }
}

$tpl = new CTestForm(
<<<HTML
<!--?>-->

<h1>TEST TEMPLATE</h1>

<form method="post" id="test">
    <h2 id="HEADER">...</h2>
    <input name="c[]" type="checkbox" value="abc"><br />
    <input name="c[]" type="checkbox" value="xyz" checked="checked"><br />
    <input name="c[]" type="checkbox" value="123"><br />
    <input name="c[]" type="hidden" instance-of="cdead">
    <select name="opt">
        <option value="123">latin</option>
        <option value="456" selected="selected">кириллица</option>
    </select>
    <div id="test"><p><input name="test" type="radio">
        <label>...</label></p></div>
    <hr>
    <input name="next[show]" type="submit" value="Show">
    <input name="next[test]" type="submit" value="Test">
    <input name="next[close]" type="submit" value="Cancel">
    <br><a href="./">Root</a>
</form>

<form method="post" id="show">
    <h2 id="HEADER">...</h2>
    <input name="next[ok]" type="submit" value="OK">
    <input name="next[close]" type="submit" value="Cancel">
</form>

<!--<?-->
HTML
,'utf-8');

//  start  //
$tpl->process('test');
?>
 </body>
</html>
